//
//  VKReManagerCashController.m
//  V-Learn
//
//  Created by liubo on 2019/1/10.
//  Copyright © 2019 yxk. All rights reserved.
//

#import "VKReManagerCashController.h"
#import "BRStringPickerView.h"
#import "WKTixianCell.h"
#import "UIColor+Hex.h"
@interface VKReManagerCashController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UILabel *moneyLabel;
@property (nonatomic, strong) UILabel *tixianLab;

@property (nonatomic, strong) UITextField *moneyTF;
@property (nonatomic, strong) UITextField *accountTF;
@property (nonatomic, strong) NSString *payType;
@property (nonatomic, strong) NSString *RMBString;/**提现金额*/
@end

@implementation VKReManagerCashController
- (UILabel *)tixianLab{
    if (!_tixianLab) {
        _tixianLab = [UILabel labelWithText:@"提现方式" andTextColor:BR_RGB_HEX(0xb9b9b9, 1.0) andName:nil andFontSize:15*kScaleFit];
    }
    return _tixianLab;
}
- (UITextField *)accountTF{
    if (!_accountTF) {
        _accountTF = [[UITextField alloc] init];
//        _accountTF.delegate = self;
        _accountTF.placeholder = @"请输入账号";
//        _accountTF.keyboardType = UIKeyboardTypeNumberPad;
        _accountTF.tag = 101;
        _accountTF.textColor =BR_RGB_HEX(0xb9b9b9, 1.0);
        
        [_accountTF setValue:[UIFont boldSystemFontOfSize:15*kScaleFit] forKeyPath:@"_placeholderLabel.font"];
    }
    return _accountTF;
}
- (UITextField *)moneyTF{
    if (!_moneyTF) {
        _moneyTF = [[UITextField alloc] init];
        _moneyTF.tag = 100;
//        _moneyTF.delegate = self;
        _moneyTF.placeholder = @"请输入提现金额";
        _moneyTF.keyboardType = UIKeyboardTypeNumberPad;
        _moneyTF.textColor = BR_RGB_HEX(0xb9b9b9, 1.0);
        [_moneyTF setValue:[UIFont boldSystemFontOfSize:15*kScaleFit] forKeyPath:@"_placeholderLabel.font"];
    }
    return _moneyTF;
}
-(UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, LBNAVBAR_HEIGHT, [UIScreen cz_screenWidth], [UIScreen cz_screenHeight] - LBNAVBAR_HEIGHT) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView];
        _tableView.backgroundColor = [UIColor cz_colorWithRed:244 green:244 blue:244];
        
//        [self setupHeaderView];
        [self setupFooterView];
    }
    return _tableView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 3;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell==nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }
    if (indexPath.row==0) {
        static NSString *cellID = @"teCell";
        WKTixianCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[WKTixianCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        cell.isNeed = YES;
        cell.isNext = YES;
        cell.textField.placeholder = @"请选择";
        cell.textField.tag = 0;
        cell.textField.delegate = self;
        cell.textField.textColor = BR_RGB_HEX(0xb9b9b9, 1.0);
        [cell.contentView addSubview:self.tixianLab];
        [self.tixianLab makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
            make.centerY.mas_equalTo(cell.contentView);
        }];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    if (indexPath.row==1) {
        [cell.contentView addSubview:self.accountTF];
        [self.accountTF makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
            make.right.mas_equalTo(-15);
            make.top.bottom.mas_equalTo(0);
        }];
    }
    if (indexPath.row==2) {
        [cell.contentView addSubview:self.moneyTF];
        [self.moneyTF makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
            make.right.mas_equalTo(-15);
            make.top.bottom.mas_equalTo(0);
        }];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
#pragma mark - UITextFieldDelegate 返回键
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    return YES;
}
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (textField.tag == 10) {
        return NO;
    } else {
        [self.view endEditing:YES];
        [self handlerTextFieldSelect:textField];
        return NO;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *header = [UIView new];
    header.backgroundColor = rgb(242,242,242);
    return header;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
- (void)handlerTextFieldSelect:(UITextField *)textField {
//    __weak typeof(self)weakSelf = self;
    switch (textField.tag) {
        case 0:
        {
            [BRStringPickerView showStringPickerWithTitle:@"请选择" dataSource:@[@"支付宝", @"银行卡",] defaultSelValue:textField.text resultBlock:^(id selectValue) {
                textField.text = selectValue;
                if ([selectValue isEqualToString:@"支付宝"]) {
                    self.payType = @"1";
                }else{
                    self.payType = @"2";
                }
            }];
            
        }
            break;
        default:{
            
        }
    }
}

- (void)payBtnClick{
    UITextField *moneyTf = [self.view viewWithTag:100];
    moneyTf.text = @"0";
}
- (void)downCountAttributedleftString:(NSString *)leftString withCenterString:(NSString *)centerString withRightString:(NSString *)rightString withLabel:(UILabel *)contentLabel {
    
    // 左边的文字颜色
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:leftString attributes:@{NSForegroundColorAttributeName:[UIColor cz_colorWithRed:152 green:152 blue:152],NSFontAttributeName:[UIFont systemFontOfSize:17.0]}];
    
    // 中间的文字
    NSMutableAttributedString *centerS = [[NSMutableAttributedString alloc]initWithString:centerString attributes:@{NSForegroundColorAttributeName:[UIColor cz_colorWithRed:253 green:79 blue:83],NSFontAttributeName:[UIFont systemFontOfSize:14.0]}];
    [string insertAttributedString:centerS atIndex:leftString.length];
    
    // 右边的文字颜色
    NSMutableAttributedString *rightS = [[NSMutableAttributedString alloc]initWithString:rightString attributes:@{NSForegroundColorAttributeName:[UIColor cz_colorWithRed:253 green:79 blue:83],NSFontAttributeName:[UIFont systemFontOfSize:17.0]}];
    [string insertAttributedString:rightS atIndex:string.length];
    contentLabel.attributedText = string;
}
- (void)setupFooterView{
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen cz_screenWidth], 280)];
    self.tableView.tableFooterView = footerView;
    
    UILabel *moneyLabel = [[UILabel alloc]init];
    _moneyLabel = moneyLabel;
    [footerView addSubview:moneyLabel];
    [moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@15);
        make.right.equalTo(@(-15));
        make.height.equalTo(@55);
        make.top.mas_equalTo(@15);
    }];
    
    [self downCountAttributedleftString:@"收益" withCenterString:@"¥" withRightString:@"0,全部提现" withLabel:moneyLabel];
    
    UIImageView *imageV = [[UIImageView alloc]init];
    imageV.image = [UIImage imageNamed:@"note"];
    [footerView addSubview:imageV];
    [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@15);
        make.width.height.equalTo(@20);
        make.top.mas_equalTo(moneyLabel).offset(150);
    }];
    UILabel *shuomLabel = [[UILabel alloc]init];
    [footerView addSubview:shuomLabel];
    shuomLabel.numberOfLines = 0;
    shuomLabel.textColor = [UIColor cz_colorWithRed:164 green:164 blue:164];
    shuomLabel.font = [UIFont systemFontOfSize:13];
    shuomLabel.text = @"请输入正确的支付宝账号或者银行卡号,一经提交不得修改。";
    [shuomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(imageV.mas_right).offset(6);
        make.centerY.equalTo(imageV);
        make.right.equalTo(-15);
    }];

    
    UIButton *commitBtn = [[UIButton alloc]init];
    [footerView addSubview:commitBtn];
    [commitBtn setImage:[UIImage imageNamed:@"vkbtn"] forState:UIControlStateNormal];
    [commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(shuomLabel.mas_bottom).offset(10);
        make.left.equalTo(@15);
        make.width.equalTo(@([UIScreen cz_screenWidth] - 30));
        make.height.equalTo(@55);
    }];
    commitBtn.imageView.contentMode = UIViewContentModeScaleToFill;
    [commitBtn addTarget:self action:@selector(commitBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *commitLabel = [[UILabel alloc]init];
    [footerView addSubview:commitLabel];
    commitLabel.textColor = [UIColor whiteColor];
    commitLabel.font = [UIFont systemFontOfSize:16];
    commitLabel.text = @"提交";
    [commitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(commitBtn).offset(15);
        make.centerX.equalTo(commitBtn);
    }];
    
}

#pragma mark - 提交接口
- (void)commitBtnClick{
    
    UITextField *accountTf = [self.view viewWithTag:101];
    UITextField *moneyTf = [self.view viewWithTag:100];
    
    NSString *accountString = [accountTf.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *moneyString = [moneyTf.text stringByReplacingOccurrencesOfString:@" " withString:@""];

    if (self.payType.length==0||self.payType==nil) {
        [MBProgressHUD showSuccess:@"请选择提现方式" toView:self.view];
        return;
    }
    if (accountString.length == 0) {
        [MBProgressHUD showSuccess:@"请输入支付宝账号或者银行卡号" toView:self.view];
        return;
    }
    if (moneyString.length == 0) {
        [MBProgressHUD showSuccess:@"请输入提现金额" toView:self.view];
        return;
    }
    
    NSInteger money = [self.RMBString integerValue];//限制金额
    NSInteger inputMoney = [moneyString integerValue];//输入金额
    NSString *infoMony = [UserModel getUserModel].earnPrice;
    NSInteger jichuMoiney = [infoMony integerValue];//个人信息金额
    //提现这里需要拿到 个人信息的里的 earnPrice 价格和输入价格比较 和后台确认
    if (jichuMoiney>=money) {

    }else{
        [MBProgressHUD showSuccess:@"暂不能提现" toView:self.view];
        return;
    }
    if (inputMoney>jichuMoiney) {
        [MBProgressHUD showSuccess:@"暂不能提现" toView:self.view];
        return;
    }
//    self.payType 1 支付  2 银行卡
    NSMutableDictionary *data = [NSMutableDictionary dictionary];
    
    [data setObject:self.payType forKey:@"accountType"];
    [data setObject:[UserModel getUserModel].token forKey:@"token"];
    [data setObject:accountString forKey:@"account"];
    [data setObject:moneyString forKey:@"withdrawMoney"];
    __weak typeof(self)weakSelf = self;
    [WYHXNetWorkManager post:WKQuerywithdrow_WKURL params:data completionHandler:^(id responseObj, NSError *error) {
        NSString *code = responseObj[@"code"];
        NSString *msg = responseObj[@"msg"];
        
        if ([code integerValue]==200) {
            
            [MBProgressHUD showSuccess:msg toView:self.view];
            
            dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0/*延迟执行时间*/ * NSEC_PER_SEC));
            
            dispatch_after(delayTime, dispatch_get_main_queue(), ^{
            [weakSelf dismissViewControllerAnimated:YES completion:nil];
            });
            
        }else if ([code integerValue]==501){
    
            [MBProgressHUD showSuccess:msg toView:self.view];
            
            return ;
        }
    }];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"提现";
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.edgesForExtendedLayout = @[];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self setupUI];
    [self tableView];
    [self requestCheckMoneData];
}

- (void)requestCheckMoneData{
    __weak typeof(self)weakSelf = self;
    //可提现金额查询
    [WYHXNetWorkManager get:WKQueryLowestMoney_WKURL params:@{@"token":[UserModel getUserModel].token} completionHandler:^(id responseObj, NSError *error) {
        NSString *code = responseObj[@"code"];
        if ([code integerValue]==200) {
            NSString *data = responseObj[@"data"];
            weakSelf.RMBString = data;
            NSString *totalMoney = [NSString stringWithFormat:@"%@,全部提现",data];
            [self downCountAttributedleftString:@"收益" withCenterString:@"¥" withRightString:totalMoney withLabel:self.moneyLabel];
        }
        [self.tableView reloadData];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)setupUI{
    
    UILabel *titleLabel = [[UILabel alloc]init];
    [self.view addSubview:titleLabel];
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.text = @"提现";
    titleLabel.font = [UIFont systemFontOfSize:19];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.mas_equalTo(self.view).offset(LBNAVBAR_HEIGHT - 40);
    }];
    
    UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    btn.backgroundColor = [UIColor clearColor];
    [self.view addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@12);
        make.width.height.equalTo(@40);
        make.centerY.equalTo(titleLabel);
    }];
    
    UIImageView *imageBackV = [[UIImageView alloc]init];
    [btn addSubview:imageBackV];
    imageBackV.image = [UIImage imageNamed:@"back"];
    imageBackV.contentMode = UIViewContentModeScaleAspectFill;
    imageBackV.userInteractionEnabled = NO;
    [imageBackV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(titleLabel);
        make.centerX.equalTo(btn);
    }];
    [btn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
  
}

- (void)buttonClick{
    [self.view endEditing:YES];
}

- (void)backAction{
    [self.view endEditing:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
@end
